﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarPark.Helper;

namespace CarPark.MainModel
{
    public partial class FrmParent : Form
    {
        //标记是否登录
        private bool hasLogIn;
        public bool HasLogIn
        {
            get { return hasLogIn; }
            set 
            {
                hasLogIn = value;
                if (hasLogIn)
                {
                    LogToolStripButton.Text = "登出";
                }
                else
                {
                    main.DealParentToMain(this, new ParentToMainEventArg(-1));
                    LogToolStripButton.Text = "登入";
                }            
            }
        }

        private FrmLogIn frmlogin = null;
        private Main main = null;

        /// <summary>
        /// 登录界面点击登录的事件处理委托
        /// </summary>
        public EventHandler loginClick;
        

        public FrmParent()
        {
            InitializeComponent();

            
            //构造登录窗体
            frmlogin = new FrmLogIn(this);
            //构造主窗体
            main = new Main(this);
            
            //自我的一些初始化
            HasLogIn = false;
            loginClick = loginClickHandle;

            frmlogin.Show();
        }


        /// <summary>
        /// 处理登陆窗体点击登录按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void loginClickHandle(object sender, EventArgs e)
        {
            LogInClickEventArg arg = e as LogInClickEventArg;
            if (null != arg)
            {
                if (arg.UserName == string.Empty)
                    MessageBox.Show(GlobalConst.PARENT_MISSING_USENAME, GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
                else if (arg.UserPassword == string.Empty)
                    MessageBox.Show(GlobalConst.PARENT_MISSING_PASSWORD, GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
                else
                {
                    int ckID;
                    string result = MidLogin.CheckLogInfo(arg.CKName, arg.UserName, arg.UserPassword,out ckID);
                    //验证用户成功
                    if (result == GlobalConst.CK_OK)
                    {
                        HasLogIn = true;
                        frmlogin.Hide();
                        MessageBox.Show(GlobalConst.PARENT_LOGIN_SUCCESS , GlobalConst.GLOBAL_POPUP_TITLE ,MessageBoxButtons.OK);
                        main.DealParentToMain(this, new ParentToMainEventArg(ckID));
                        main.Show();
                    }
                    else
                        MessageBox.Show(GlobalConst.PARENT_LOGIN_FAILED, GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
                }               
            }
            else
                MessageBox.Show(GlobalConst.GLOBAL_MISSING_ARG, GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
        }


        private void LogToolStripButton_Click(object sender, EventArgs e)
        {
            //如果已经登录，则退出登录，关闭所有窗口，标记为登出状态，显示登录对话框
            if (HasLogIn)
            {
                main.Hide();
                frmlogin.Show();
                HasLogIn = false;
            }
            //如果是登出状态，则显示登录界面
            else
            {
                main.Hide();
                frmlogin.Show();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GlobalConst.ABOUT, GlobalConst.GLOBAL_POPUP_TITLE, MessageBoxButtons.OK);
        }

        private void helpToolStripButton_Click(object sender, EventArgs e)
        {
            aboutToolStripMenuItem_Click(this, e);
        }


        #region private method


        #endregion

    }


    public class ParentToMainEventArg : EventArgs
    {
        public ParentToMainEventArg(int ckid) : base()
        {
            CKID = ckid;
        }
        public int CKID = -1;
    }


}
